clear all
set more off
global data 	"R:\SharedProjects\Shared2020-070\2016\extend_to_2020\JPE_Replication_dta"
global figures 	"R:\SharedProjects\Shared2020-070\2016\extend_to_2020\JPE_Replication_log"

cap log close
log using $figures\E_file,replace t


cd $data

**********************************************************************************************************************************************************************************
*******THE DATASET complete_data_final_v1.dta
*******is N(SSA)*N(F831) for those with a record in F831, where N(SSA)=# of SSA records from 1978 to 2020, and 
*******N(F831) is the # of records in the F831 (cycles*rounds)
*******and N(SSA) for those without a record in F831
**********************************************************************************************************************************************************************************


**********************************************************************************************************************************************************************************
**************************************THIS FILE IDENTIFIES IF PEOPLE ARE, IN A GIVEN YEAR, IN "ROUND 1-APPLICANT STATUS"
**************************************THERE WILL BE 1 RECORD PER YEAR (AT MOST) - 0 FOR NON-APPLICANTS OR ROUND 2 APPLICANTS
**********************************************************************************************************************************************************************************
u complete_data_final_v1, clear

sort hhidpn rid cycleid round, stable
by hhidpn rid cycleid (round): gen date_dec_finalround = date_dec[_N] if round[_N]>1		/*Date final decision for those with round 2, i.e., appealants. AL=round */
																							/*For people who have ***only*** a round-1, this is missing*/

drop if round!=1 & round!=. 																/*Drop all later rounds (for those who have a F831 record). */
																							/*Application analysis only refers to 1st round!!! */
																							/*This keeps F831 round-1 record + non-F831 records */
																							/*Adjudicative level =1 throughout*/	

gen finalround_decision_year	= year(date_dec_finalround)
gen year0 						= year(date_app)
ren year_dec firstround_decision_year		

keep if year==year0																			/*Keep only obs in the year of application, drop the non-F831 people at this point*/

gen year_diff 		= finalround_decision_year-year0										/*Years between 1st round application and final decision - for appealants*/	
replace year_diff 	= firstround_decision_year-year0 if year_diff==.						/*Years between 1st round application and final decision - for non-appealants*/
																							/*This helps us distinguish between those who're waiting for the decision on*/
																							/*years after their application*/			

drop if year_diff>5 & year_diff!=. 															/*Drop obs with more than 5 years between application and final decision*/
drop if year_diff<0 & year_diff!=. 															

gen year1= year0+1
gen year2= year0+2
gen year3= year0+3
gen year4= year0+4
gen year5= year0+5
replace year1=. if year_diff <1
replace year2=. if year_diff <2
replace year3=. if year_diff <3
replace year4=. if year_diff <4
replace year5=. if year_diff <5
drop year

reshape long year, i(hhidpn record) j(yr_since_app)
drop if year==.																				/*This keeps only the years in which people are either applying or waiting*/
																							/*The var. 	yr_since_app=0 if this is the year of application*/
																							/*and is=3, say, if you are in your 3rd year waiting for the final decision*/


*The next two lines identify years in which applicant is in "appeal" status ("finalround_decision_year" comes from rounds 2, 3, etc.)
gen 	activeappeal=0
replace activeappeal=1 if year>firstround_decision_year & year<=finalround_decision_year & finalround_decision_year!=.    

g applied_current	=yr_since==0															/*Applicant, in year of application*/
g applied_waiting	=yr_since>0 & active==0													/*Applicant, still waiting for decision in year beyond year of application*/
g appealed			=active==1																/*Appealant*/

keep hhidpn year applied_* appealed cycleid
ren cycleid cycleid_applied

gen applied=applied_current+applied_waiting													
duplicates drop hhidpn year applied,force													/*If the person has two applications in a given year, he will be an "applicant" */
																							/*for that year, so no need for duplicates*/
drop applied																				/*The applied variable only used to drop duplicates, carry applied_current, appied_waiting, appealant*/

egen n=sum(hhidpn!=.),by(hhidpn year)
drop if n==2 & appealed==1																	/*These are people who, in that year, are both applicants and appealant. */
																							/*Keeps only the "applicant" record. */
drop n												

sort hhidpn year, stable
save temp_applied,replace																	/*These are people who are identified to be in "active application" status */
																							/*in that year (applied this year, applied last year(s) & waiting, 
																							appealing a decision)*/
**********************************************************************************************************************************************************************************
**********************************************************************************************************************************************************************************


**********************************************************************************************************************************************************************************
**************************************THIS FILE IDENTIFIES ROUND 1 APPLICATION RECORDS THAT CAN BE MATCHED TO HRS (WITHIN 12 MONTHS OF INTVW)
**************************************IT MAY INCLUDE MULTIPLE YEAR RECORDS PER INDIVIDUAL IF THE INDIVIDUAL SUBMITTED MULTIPLE APPLICATIONS
**************************************(FOR EX., APPLICATION IN FEB, DECISION IN JUNE, NEW APPLICATION IN AUG, HRS INTERVIEW IN OCT)
**********************************************************************************************************************************************************************************
cd $data
u complete_data_final_v1.dta, clear

keep if round==1 						// keep only first round of an application cycle (and hence *only* people with F831 records)
keep if year==year_app 					// only keep observation from application year

** Define sample
keep if within_threshold_appl==1 		// only keep if HRS interview within 12 months of application
drop if (age<20 | age>65) & age!=. 		// keep only those in relevant age range
drop if proxy_appl == 1 				//drop proxy interviews

replace bmi_appl=. if bmi_appl>106		/*John Brower Minnoch had bmi of 105.3 - WORLD RECORD! */

gen sample=0
replace sample=1 if success!=. & nosuccess!=. & rid!=. & hlthlm_appl!=. & college!=. & raracem!=. & married_appl!=. & widowed_appl!=. & experience!=. & age!=. &  female!=.  & bs!=.  & bmi_appl!=.

g ssi_only	=rid==16
g di_only	=rid==2
g ssi_and_di=rid==216

keep hhidpn year record round nosuccess ssi_only ssi_and_di sample
sort hhidpn year
save temp_reject,replace	



*********************************************************************************************************************************************************************************
**************************************THIS TAKES MAIN FILE, MERGES IN "APPLICANT STATUS" AND HRS-MATCHABLE APPLICATION RECORDS FOR USE IN THE APPLICATION/REJECTION ANALYSIS
*********************************************************************************************************************************************************************************
cd $data
u complete_data_final_v1, clear

drop if round==2												/*use only round 1*/
gen record_app=year==year_app									/*application year*/
gen record_new=record											/*F831 records - there are as many 1978-2020 blocks as F831 records for a given individual*/	
														
replace record_new=0 if record_new!=. & record_app==0			/*This will set record_new in year of application to be 1,2,3,etc., and non-application year records to be 0*/
duplicates drop hhidpn record_new year,force					/*This keeps non-F831 people; and for the F831 people keeps 1 record if non-application, */
																/*x+1 records per year if there is are x round-1 applications in that year*/
egen mmm=sum(hhidpn!=.),by(hhidpn year)
drop if record_new==0 & mmm>1									/*For the application years, drop the non-application records*/
drop mmm
egen mmm=sum(hhidpn!=.),by(hhidpn year)							/*There are a few people with 2,3 applications in a given year (distinct records). Since we are only interested*/
																/*in whether that person is an "applicant" that year, one record suffices*/
duplicates drop hhidpn year mmm,force					
drop mmm record_app												

sort hhidpn year
merge hhidpn year using temp_applied
drop _merge

replace applied_current=0 if applied_current==.					/*HRS people with no record in F831 - They are non-applicants b/c they are consenters */
replace applied_waiting=0 if applied_waiting==.
replace appealed=0 if appealed==.

* drop if has died
ren earn ern

drop if year> year_died & year_died!=.							

/** Define sample
We want to keep everyone who 
(1) either has no active application (as per F831) and is not receiving both SSI and DI benefits in given calendar year
(2) or has an active application in first round of application cycle in given calendar year
and
(3) can be merged to HRS interview in given calendar year
**/

gen applied=applied_c+applied_w									

gegen mniw=min(interview_year),by(hhidpn)						/*First HRS interview year*/
gegen mxiw=max(interview_year),by(hhidpn)						/*Last HRS interview year*/
gen dy=year-mniw

***These keeps in people who applied the year before their first HRS intvw
gegen mniw2=min(year) if applied==1,by(hhidpn)
gegen mniw3=mean(mniw2),by(hhidpn)
replace mniw=mniw3 if mniw>mniw3 & mniw!=. & mniw3!=.			
drop mniw2 mniw3

keep if year>=mniw & year<=mxiw									
drop mniw mxiw

drop if interview_year==. & (applied_c+applied_w+appealed==0)	/*These are the odd-year datapoints for non-F831-people and the odd-year datapoints for the F831-people when not applying */

**This keeps people who can be merged in HRS the same year or those in odd years who applied in that year 
**These people, by design, get assigned the interview value of the next coming HRS wave (same principle adopted for the Type I regression)
keep if (year==interview_year)|(year!=interview_year & applied==1)		

gen apply_odd_year=year!=interview_year & applied==1			/*These are dummies for the odd-year datapoints for F831-people */

*************SAMPLE********************************************************************************************************
drop if (age<20 | age>65) & age!=. 								/*keep only those in relevant age range*/
																/*drop if in appeal and no active application that year*/
drop if appealed==1
****************************************************************************************************************************

sort hhidpn year 
merge hhidpn year using temp_reject
drop if _merge==2
gen type12_record=_merge==3										/*these are records F831 matchable with HRS (ie within 12 months of intvw)*/
drop _merge

drop if dy<0 & type12_record==0									/*Don't need years before HRS first intvw for non-F831 people*/
																/*for F831 people, dy=-1 or more*/

gen mistimed_apply	=applied==1 & type12_record==0				
replace applied=0 if applied==1 & type12_record==0				

drop if (proxy_appl==1 & type12==1)								/*Drop proxy interviews*/
drop if (proxy_intvw==1 & type12==0)

drop longest_occ_combined_x 

keep 	hhidpn year wave female college raracem experience* bs_sf_* married_* widowed_* age walkra_* dressa_* stoopa_* beda_* bmi_* hosp_* longest_occ_combined_* ///
		mistimed_apply applied type12_record apply_odd_year interview_year nosuccess ssi_only ssi_and_di sample bs hlthlm_* cantwork_* hlth_temp_* ///
		higov_* prpcnt_* prpcnt_* hiothp_* covr_* oopmd_* oopmdo_* oopmdsp_* oopmdosp_* achck_* itot_* gisp_* pisp_* issi_* issdi_* covhelp_* year_died astck* acd* abond* totmd_* ///
		covhosp_* covnurs_* covsurg_* covdoct_* covdent_* covdrug_* covhome_* mrprem_*_real prprm1_*_real prprm2_*_real prprm3_*_real
		
foreach x in 	bs_sf_ married_ widowed_ walkra_ dressa_ stoopa_ beda_ bmi_ hosp_ longest_occ_combined_ ///
				hlthlm_ cantwork_ hlth_temp_ higov_ prpcnt_ hiothp_ covr_ gisp_ pisp_ covhelp_ covhosp_ ///
				covnurs_ covsurg_ covdoct_ covdent_ covdrug_ covhome_ mrprem_ prprm1_ prprm2_ prprm3_	{
		gen `x'R=.
		replace `x'R=`x'appl 	if type12==1
		replace `x'R=`x'intvw 	if type12==0
				}

rename *_real *
		
foreach x in oopmd_ oopmdo_ oopmdsp_ oopmdosp_ achck_ itot_ issi_ issdi_  astck_ acd_ abond_ totmd_  {
		gen `x'R=.
		replace `x'R=`x'appl 	if type12==1
		replace `x'R=`x'intvw 	if type12==0
				}
		
gen 	cantwork_notemp=hlthlm_R==1 & cantwork_R==1 & hlth_temp_R==0
gen 	received_ssidi=(issi_R>0 & issi_R<.)|(issdi_R>0 & issdi_R<.)

gen doctor_told_has_hbp=bs_sf_R==1
gen doctor_told_has_psy=bs_sf_R==2
gen doctor_told_has_hea=bs_sf_R==3
gen doctor_told_has_art=bs_sf_R==4
gen doctor_told_has_dia=bs_sf_R==5
gen doctor_told_has_lun=bs_sf_R==6
gen doctor_told_has_str=bs_sf_R==7
gen doctor_told_has_can=bs_sf_R==8

* ADL recodes
foreach i in walkra_R dressa_R stoopa_R beda_R hosp_R {
	gen `i'_1 = 0
	replace `i'_1 = 1 if `i' == 1
}

g occ_simple=.
replace occ_simple=1 if longest_occ_combined_R>=1  & longest_occ_combined_R<=4	/*managers and clerical*/
replace occ_simple=2 if longest_occ_combined_R>=5  & longest_occ_combined_R<=9	/*service jobs*/
replace occ_simple=3 if longest_occ_combined_R>=10 & longest_occ_combined_R<=12	/*construction, mining, mechanics*/
replace occ_simple=4 if longest_occ_combined_R>=13 & longest_occ_combined_R<=16	/*operators*/
replace occ_simple=5 if longest_occ_combined_R==0   |longest_occ_combined_R==17	/*others/unknown/armed forces*/

gen govt_hins=(higov_R==1)
gen priv_hins=(prpcnt_R>0 & prpcnt_R<.)|(hiothp_R==1)|(covr_R==1)

gen x=(oopmd_R/2)								/*Data are for previous 2 years, get annual amounts*/
gen x2=x
replace x2=(oopmdo_R/2)	if wave>=10 & oopmdo_R!=.
gen x3=x
replace x3=(oopmdo_R/2)	if wave>=10 & oopmd_R==0 & oopmdo_R!=. 

gen logx=log(x)

egen x_w_ins=rsum(oopmd_R mrprem_R prprm1_R prprm2_R prprm3_R)
gen logx_w_ins=log(x_w_ins)

gen totx=totmd_R
replace totx=x if x!=0 & totx==0 & wave==3
gen logtotx=log(totx)

gen lowcash=achck_R<2000
gen logy=log(itot_R)

tab raracem,gen(racex)
tab longest_occ_combined_R,gen(longestoccx)
tab bs,gen(bsx)

drop if applied==0 & apply_odd==1												/*This drops some of the mistimed, correctly so*/

replace nosuccess=. if applied==0

keep if received_ssidi==0
drop if year_died<=2006

replace astck_R=0 if astck_R<0
gen logbs=log(itot_R+achck_R+astck_R+acd_R+abond_R)
replace lowcash		=achck_R<500

egen spin=rmax(pisp_R gisp_R)

egen somecov1=rmax(covdoct_R covdent_R covdrug_R)
egen somecov2=rmax(covhosp_R covnurs_R covsurg_R covhome_R)

**Spouse medical spending (intensive and extensive margins)
gen 	sp_medexp=oopmdsp_R>0 & oopmdsp_R!=.
replace sp_medexp=1 if sp_medexp==0 & wave>=10 & oopmdosp_R>0 & oopmdosp_R!=.
gen 	xsp=oopmdsp_R
gen 	sp_medexp2=xsp*sp_medexp

g occ_interm=.
replace occ_interm=1 if longest_occ_combined_R==0								/*Unknown*/
replace occ_interm=2 if longest_occ_combined_R>=1  & longest_occ_combined_R<=2	/*Manag/Prof*/
replace occ_interm=3 if longest_occ_combined_R>=3  & longest_occ_combined_R<=4	/*Sales/clerical*/
replace occ_interm=4 if longest_occ_combined_R>=5  & longest_occ_combined_R<=7	/*Clean/Protect/Food serv*/
replace occ_interm=5 if longest_occ_combined_R>=8  & longest_occ_combined_R<=9	/*Personal/Health serv*/
replace occ_interm=6 if longest_occ_combined_R>=10 & longest_occ_combined_R<=12	/*Farm/Constr/Mech*/
replace occ_interm=7 if longest_occ_combined_R==13 | longest_occ_combined_R==17	/*Precision/Armed force*/
replace occ_interm=8 if longest_occ_combined_R>=14 & longest_occ_combined_R<=16	/*operators*/

rename married_R married
rename widowed_R widowed 

drop if mistimed		==1			
replace experience=0 if experience==.
drop if racex2==.
drop if bmi_R>110
drop if hosp_R>1
drop if logbs==.
drop if college==.

keep 	hhidpn year wave female college racex2 age experience married widowed doctor* walkra_R_1 dressa_R_1 stoopa_R_1 beda_R_1 bmi_R hosp_R occ_interm ///
		nosuccess cantwork_notemp type12 received_ssidi mistimed bsx* govt_hins priv_hins logy lowcash govt_hins priv_hins covhelp_R pisp_R gisp_R x logx ///
		applied logbs sp_medexp* spin somecov*

*********************************************************
*********************************************************
save rejection_data,replace
*********************************************************
***THIS IS THE MAIN FILE USED FOR THE STRUCTURAL ANALYSIS
*********************************************************



erase temp_reject.dta


log close